#include "mainwindow.h"
#include <QPainter>
#include <QMouseEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), progress(0)
{
    setFixedSize(200, 200);
    int sideLenght = 45;
    int centerX = width() / 2;
    int centerY = height() / 4 - 5;

    triangleButton << QPoint(centerX - sideLenght / 2, centerY + sideLenght / 2)
                   << QPoint(centerX + sideLenght / 2, centerY + sideLenght / 2)
                   << QPoint(centerX, centerY - sideLenght / 2);

    squareButton = QRect(25,height() / 2 - 25, 50, 50);
    circleButton = QRect(width() - 70, height() / 2 - 25 , 50, 50);
    crossButton << QPoint(width() / 2, height() - 75)
                << QPoint(width() / 2 + 20, height() - 50)
                << QPoint(width() / 2, height() - 25)
                << QPoint(width() / 2 - 20, height() - 50);
}

void MainWindow::setProgress(int value) {
    if ((value < 0) || (value > 100)) {
        return;
    }
    progress = value;
    update();
}

void MainWindow::paintEvent(QPaintEvent*) {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);


    QRectF rectangleall(20.0,20.0,width() - 40, height() - 40);
    painter.setBrush(QBrush(Qt::gray));
    painter.setPen(Qt::NoPen);
    painter.drawEllipse(rectangleall);
    QRectF rectangle(10.0, 10.0, width() - 20, height() - 20);
    int startAngel = 90 * 16;
    int spanAngle = progress * 3.6 * 16;

    painter.setPen(Qt::black);
    painter.drawEllipse(rectangle);

    QPen pen (Qt::blue, 15);
    painter.setPen(pen);
    painter.drawArc(rectangle, startAngel, -spanAngle);
    QPen triangle (Qt::green);
    painter.setPen(triangle);
    drawTriangleButton(painter);

    painter.setBrush(QBrush(Qt::magenta));
    painter.setPen(Qt::NoPen);
    painter.drawRect(squareButton);

    painter.setBrush(QBrush(Qt::red));
    painter.drawEllipse(circleButton);

    painter.setBrush(QBrush(Qt::blue));
    painter.drawPolygon(crossButton);
}


void MainWindow::mousePressEvent(QMouseEvent *event) {
    if (triangleButton.containsPoint(event->pos(), Qt::OddEvenFill)) {
        setProgress(progress + 10);
        return;
    }
    if (squareButton.contains(event->pos(),Qt::OddEvenFill)) {
        setProgress(progress + 5);
        return;
    }
    if (circleButton.contains(event->pos(),Qt::OddEvenFill)) {
        setProgress(progress - 5);
        return;
    }
    if (crossButton.containsPoint(event->pos(),Qt::OddEvenFill)) {
        setProgress(progress - 10);
        return;
    }
}

void MainWindow::keyPressEvent(QKeyEvent *event) {
    if (event->key() == Qt::Key_Up) {
        setProgress(progress + 10);
    } else if (event->key() == Qt::Key_Down) {
        setProgress(progress - 10);
    }
}

void MainWindow::drawTriangleButton(QPainter &painter) {
    painter.setBrush(Qt::green);
    painter.drawPolygon(triangleButton);
}
MainWindow::~MainWindow()
{
}
